package com.gusto.engine.beans.aspects.statistics;

import org.apache.log4j.Logger;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Configurable;

import com.gusto.engine.beans.aspects.cache.CacheAspect;
import com.gusto.engine.beans.model.Res;

/**
 * <p>Decorates the {@link CacheAspect} and adds Statistics capabilities.
 * <ul>
 *   <li>Number of properties calculated with a request to the underlying data-source.
 *   <li>Number of properties calculated from cache.
 * </ul>
 * The actual implementation only considers the global statistics for all {@link Res}.</p>
 * 
 * @author amokrane.belloui@gmail.com
 *
 */
@Aspect
@Configurable
public class StatisticsAspect {
	
	private Logger log = Logger.getLogger(getClass());
	
	private ResStatistics resStats;
	
	public StatisticsAspect() {
		super();
		log.info("StatisticsAspect initialized " + this);
	}

	public ResStatistics getResStats() {
		return resStats;
	}

	public void setResStats(ResStatistics resStats) {
		this.resStats = resStats;
		log.info("Injecting ResStatistics " + resStats);
	}
	
	@After("execution(* com.gusto.engine.beans.aspects.cache.CacheAspect.returnFromRepos(..))")
	public void plusFromRepos() {
		if (resStats != null) {
			resStats.increaseFromRepos();
			log.info("From Repository ++ " + resStats.getFromRepos());
		}
	}
	
	@After("execution(* com.gusto.engine.beans.aspects.cache.CacheAspect.returnFromCache(..))")
	public void plusFromCache() {
		if (resStats != null) {
			resStats.increaseFromCache();
			log.info("From Cache ++" + resStats.getFromCache());
		}
	} 
	
}